﻿import pygame
import sys
import math
from pygame.locals import *

# 初始化 Pygame
pygame.init()

# 屏幕设置
WIDTH, HEIGHT = 1000, 700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('拼音字母表学习工具')

# 颜色定义
BACKGROUND = (240, 248, 255)  # 淡蓝色背景
TEXT_COLOR = (25, 25, 112)    # 深蓝色文字
HIGHLIGHT = (255, 140, 0)     # 橙色高亮
GRID_COLOR = (200, 200, 230)  # 淡紫色网格
BUTTON_COLOR = (70, 130, 180)  # 钢蓝色按钮
BUTTON_HOVER = (100, 149, 237)  # 矢车菊蓝悬停
CARD_COLOR = (255, 250, 240)  # 卡片底色

# 字体
title_font = pygame.font.SysFont("simhei", 48, bold=True)
subtitle_font = pygame.font.SysFont("simhei", 36)
text_font = pygame.font.SysFont("simhei", 24)
small_font = pygame.font.SysFont("simhei", 20)
letter_font = pygame.font.SysFont("Arial", 72, bold=True)
pinyin_font = pygame.font.SysFont("simhei", 32)

# 时钟
clock = pygame.time.Clock()
FPS = 60

# 拼音字母表数据
PINYIN_DATA = {
    "声母": [
        {"letter": "b", "upper": "B", "example": "爸爸 (bàba)", "sound": "b"},
        {"letter": "p", "upper": "P", "example": "苹果 (píngguǒ)", "sound": "p"},
        {"letter": "m", "upper": "M", "example": "妈妈 (māma)", "sound": "m"},
        {"letter": "f", "upper": "F", "example": "飞机 (fēijī)", "sound": "f"},
        {"letter": "d", "upper": "D", "example": "大象 (dàxiàng)", "sound": "d"},
        {"letter": "t", "upper": "T", "example": "太阳 (tàiyáng)", "sound": "t"},
        {"letter": "n", "upper": "N", "example": "牛奶 (niúnǎi)", "sound": "n"},
        {"letter": "l", "upper": "L", "example": "老师 (lǎoshī)", "sound": "l"},
        {"letter": "g", "upper": "G", "example": "哥哥 (gēge)", "sound": "g"},
        {"letter": "k", "upper": "K", "example": "考拉 (kǎolā)", "sound": "k"},
        {"letter": "h", "upper": "H", "example": "花朵 (huāduǒ)", "sound": "h"},
        {"letter": "j", "upper": "J", "example": "家庭 (jiātíng)", "sound": "j"},
        {"letter": "q", "upper": "Q", "example": "气球 (qìqiú)", "sound": "q"},
        {"letter": "x", "upper": "X", "example": "西瓜 (xīguā)", "sound": "x"},
        {"letter": "zh", "upper": "ZH", "example": "中国 (Zhōngguó)", "sound": "zh"},
        {"letter": "ch", "upper": "CH", "example": "吃饭 (chīfàn)", "sound": "ch"},
        {"letter": "sh", "upper": "SH", "example": "书包 (shūbāo)", "sound": "sh"},
        {"letter": "r", "upper": "R", "example": "日本 (Rìběn)", "sound": "r"},
        {"letter": "z", "upper": "Z", "example": "自行车 (zìxíngchē)", "sound": "z"},
        {"letter": "c", "upper": "C", "example": "草莓 (cǎoméi)", "sound": "c"},
        {"letter": "s", "upper": "S", "example": "松鼠 (sōngshǔ)", "sound": "s"},
        {"letter": "y", "upper": "Y", "example": "月亮 (yuèliang)", "sound": "y"},
        {"letter": "w", "upper": "W", "example": "玩具 (wánjù)", "sound": "w"}
    ],
    "韵母": [
        {"letter": "a", "upper": "A", "example": "阿姨 (āyí)", "sound": "a"},
        {"letter": "o", "upper": "O", "example": "喔喔 (ōō)", "sound": "o"},
        {"letter": "e", "upper": "E", "example": "鹅 (é)", "sound": "e"},
        {"letter": "i", "upper": "I", "example": "衣服 (yīfu)", "sound": "i"},
        {"letter": "u", "upper": "U", "example": "乌龟 (wūguī)", "sound": "u"},
        {"letter": "ü", "upper": "Ü", "example": "雨伞 (yǔsǎn)", "sound": "ü"},
        {"letter": "ai", "upper": "AI", "example": "爱心 (àixīn)", "sound": "ai"},
        {"letter": "ei", "upper": "EI", "example": "杯子 (bēizi)", "sound": "ei"},
        {"letter": "ui", "upper": "UI", "example": "围巾 (wéijīn)", "sound": "ui"},
        {"letter": "ao", "upper": "AO", "example": "奥运 (àoyùn)", "sound": "ao"},
        {"letter": "ou", "upper": "OU", "example": "海鸥 (hǎi'ōu)", "sound": "ou"},
        {"letter": "iu", "upper": "IU", "example": "秋天 (qiūtiān)", "sound": "iu"},
        {"letter": "ie", "upper": "IE", "example": "爷爷 (yéye)", "sound": "ie"},
        {"letter": "üe", "upper": "ÜE", "example": "月亮 (yuèliang)", "sound": "üe"},
        {"letter": "er", "upper": "ER", "example": "耳朵 (ěrduo)", "sound": "er"},
        {"letter": "an", "upper": "AN", "example": "安全 (ānquán)", "sound": "an"},
        {"letter": "en", "upper": "EN", "example": "恩情 (ēnqíng)", "sound": "en"},
        {"letter": "in", "upper": "IN", "example": "音乐 (yīnyuè)", "sound": "in"},
        {"letter": "un", "upper": "UN", "example": "温度 (wēndù)", "sound": "un"},
        {"letter": "ün", "upper": "ÜN", "example": "云彩 (yúncai)", "sound": "ün"},
        {"letter": "ang", "upper": "ANG", "example": "阳光 (yángguāng)", "sound": "ang"},
        {"letter": "eng", "upper": "ENG", "example": "风筝 (fēngzheng)", "sound": "eng"},
        {"letter": "ing", "upper": "ING", "example": "樱桃 (yīngtáo)", "sound": "ing"},
        {"letter": "ong", "upper": "ONG", "example": "恐龙 (kǒnglóng)", "sound": "ong"}
    ],
    "整体认读音节": [
        {"letter": "zhi", "upper": "ZHI", "example": "知识 (zhīshi)", "sound": "zhi"},
        {"letter": "chi", "upper": "CHI", "example": "吃饭 (chīfàn)", "sound": "chi"},
        {"letter": "shi", "upper": "SHI", "example": "时间 (shíjiān)", "sound": "shi"},
        {"letter": "ri", "upper": "RI", "example": "日本 (Rìběn)", "sound": "ri"},
        {"letter": "zi", "upper": "ZI", "example": "字典 (zìdiǎn)", "sound": "zi"},
        {"letter": "ci", "upper": "CI", "example": "词语 (cíyǔ)", "sound": "ci"},
        {"letter": "si", "upper": "SI", "example": "思想 (sīxiǎng)", "sound": "si"},
        {"letter": "yi", "upper": "YI", "example": "衣服 (yīfu)", "sound": "yi"},
        {"letter": "wu", "upper": "WU", "example": "舞蹈 (wǔdǎo)", "sound": "wu"},
        {"letter": "yu", "upper": "YU", "example": "宇宙 (yǔzhòu)", "sound": "yu"},
        {"letter": "ye", "upper": "YE", "example": "爷爷 (yéye)", "sound": "ye"},
        {"letter": "yue", "upper": "YUE", "example": "月亮 (yuèliang)", "sound": "yue"},
        {"letter": "yuan", "upper": "YUAN", "example": "公园 (gōngyuán)", "sound": "yuan"},
        {"letter": "yin", "upper": "YIN", "example": "音乐 (yīnyuè)", "sound": "yin"},
        {"letter": "yun", "upper": "YUN", "example": "云彩 (yúncai)", "sound": "yun"},
        {"letter": "ying", "upper": "YING", "example": "樱桃 (yīngtáo)", "sound": "ying"}
    ]
}

# 按钮类
class Button:
    def __init__(self, x, y, width, height, text):
        self.rect = pygame.Rect(x, y, width, height)
        self.text = text
        self.color = BUTTON_COLOR
        self.hover = False
    
    def draw(self, surface):
        # 绘制按钮背景
        color = BUTTON_HOVER if self.hover else self.color
        pygame.draw.rect(surface, color, self.rect, border_radius=10)
        pygame.draw.rect(surface, TEXT_COLOR, self.rect, 2, border_radius=10)
        
        # 绘制按钮文本
        text = text_font.render(self.text, True, TEXT_COLOR)
        text_rect = text.get_rect(center=self.rect.center)
        surface.blit(text, text_rect)
    
    def is_clicked(self, pos):
        return self.rect.collidepoint(pos)
    
    def update_hover(self, pos):
        self.hover = self.rect.collidepoint(pos)

# 拼音卡片类
class PinyinCard:
    def __init__(self, x, y, width, height, data):
        self.rect = pygame.Rect(x, y, width, height)
        self.data = data
        self.highlight = False
    
    def draw(self, surface):
        # 绘制卡片背景
        color = HIGHLIGHT if self.highlight else CARD_COLOR
        pygame.draw.rect(surface, color, self.rect, border_radius=10)
        pygame.draw.rect(surface, TEXT_COLOR, self.rect, 2, border_radius=10)
        
        # 绘制字母
        letter_text = letter_font.render(self.data["letter"], True, TEXT_COLOR)
        surface.blit(letter_text, (self.rect.centerx - letter_text.get_width() // 2, self.rect.y + 20))
        
        # 绘制大写字母
        upper_text = small_font.render(f"大写: {self.data['upper']}", True, TEXT_COLOR)
        surface.blit(upper_text, (self.rect.centerx - upper_text.get_width() // 2, self.rect.y + 100))
        
        # 绘制示例词语
        example_text = text_font.render(self.data["example"], True, TEXT_COLOR)
        surface.blit(example_text, (self.rect.centerx - example_text.get_width() // 2, self.rect.y + 140))
        
        # 绘制发音提示
        sound_text = small_font.render(f"发音: {self.data['sound']}", True, TEXT_COLOR)
        surface.blit(sound_text, (self.rect.centerx - sound_text.get_width() // 2, self.rect.y + 180))

# 拼音学习工具
class PinyinLearningTool:
    def __init__(self):
        self.current_category = "声母"
        self.cards = []
        self.buttons = []
        self.selected_card = None
        self.create_buttons()
        self.create_cards()
    
    def create_buttons(self):
        # 创建分类按钮
        categories = ["声母", "韵母", "整体认读音节"]
        button_width = 200
        button_height = 50
        button_margin = 20
        start_x = WIDTH // 2 - (button_width * 3 + button_margin * 2) // 2
        
        for i, category in enumerate(categories):
            x = start_x + i * (button_width + button_margin)
            y = HEIGHT - 100
            self.buttons.append(Button(x, y, button_width, button_height, category))
    
    def create_cards(self):
        self.cards = []
        category_data = PINYIN_DATA[self.current_category]
        
        # 计算卡片布局
        cards_per_row = 5
        card_width = 150
        card_height = 220
        card_margin = 20
        start_x = (WIDTH - (cards_per_row * card_width + (cards_per_row - 1) * card_margin)) // 2
        start_y = 150
        
        # 创建卡片
        for i, item in enumerate(category_data):
            row = i // cards_per_row
            col = i % cards_per_row
            x = start_x + col * (card_width + card_margin)
            y = start_y + row * (card_height + card_margin)
            self.cards.append(PinyinCard(x, y, card_width, card_height, item))
    
    def set_category(self, category):
        self.current_category = category
        self.create_cards()
        self.selected_card = None
    
    def draw(self, surface):
        # 绘制标题
        title = title_font.render("拼音字母表学习工具", True, TEXT_COLOR)
        surface.blit(title, (WIDTH // 2 - title.get_width() // 2, 20))
        
        # 绘制当前分类
        category_text = subtitle_font.render(f"当前分类: {self.current_category}", True, HIGHLIGHT)
        surface.blit(category_text, (WIDTH // 2 - category_text.get_width() // 2, 80))
        
        # 绘制卡片
        for card in self.cards:
            card.draw(surface)
        
        # 绘制按钮
        for button in self.buttons:
            button.draw(surface)
        
        # 绘制说明
        instruction = text_font.render("点击字母卡片查看详情，点击下方按钮切换分类", True, TEXT_COLOR)
        surface.blit(instruction, (WIDTH // 2 - instruction.get_width() // 2, HEIGHT - 50))
        
        # 绘制详细信息
        if self.selected_card:
            self.draw_detail(surface)
    
    def draw_detail(self, surface):
        if not self.selected_card:
            return
        
        # 创建半透明覆盖层
        overlay = pygame.Surface((WIDTH, HEIGHT), pygame.SRCALPHA)
        overlay.fill((0, 0, 0, 150))
        surface.blit(overlay, (0, 0))
        
        # 创建详情卡片
        detail_width = 600
        detail_height = 400
        detail_x = (WIDTH - detail_width) // 2
        detail_y = (HEIGHT - detail_height) // 2
        detail_rect = pygame.Rect(detail_x, detail_y, detail_width, detail_height)
        
        # 绘制详情卡片背景
        pygame.draw.rect(surface, CARD_COLOR, detail_rect, border_radius=15)
        pygame.draw.rect(surface, TEXT_COLOR, detail_rect, 2, border_radius=15)
        
        # 绘制字母
        letter = self.selected_card.data["letter"]
        upper = self.selected_card.data["upper"]
        example = self.selected_card.data["example"]
        sound = self.selected_card.data["sound"]
        
        # 字母标题
        title_text = title_font.render(f"{letter} ({upper})", True, HIGHLIGHT)
        surface.blit(title_text, (detail_rect.centerx - title_text.get_width() // 2, detail_y + 30))
        
        # 发音
        sound_text = pinyin_font.render(f"发音: {sound}", True, TEXT_COLOR)
        surface.blit(sound_text, (detail_rect.centerx - sound_text.get_width() // 2, detail_y + 100))
        
        # 示例词语
        example_text = text_font.render(f"示例词语: {example}", True, TEXT_COLOR)
        surface.blit(example_text, (detail_rect.centerx - example_text.get_width() // 2, detail_y + 160))
        
        # 发音方法
        sound_method = self.get_sound_method(letter)
        method_text = text_font.render(f"发音方法: {sound_method}", True, TEXT_COLOR)
        surface.blit(method_text, (detail_rect.centerx - method_text.get_width() // 2, detail_y + 200))
        
        # 书写方法
        writing_text = text_font.render("书写方法: 按照字母形状书写，注意大小写区别", True, TEXT_COLOR)
        surface.blit(writing_text, (detail_rect.centerx - writing_text.get_width() // 2, detail_y + 240))
        
        # 关闭按钮
        close_button = Button(detail_rect.centerx - 50, detail_y + detail_height - 70, 100, 40, "关闭")
        close_button.draw(surface)
        
        return close_button
    
    def get_sound_method(self, letter):
        # 简化的发音方法描述
        methods = {
            "b": "双唇闭合，然后突然张开，发出爆破音",
            "p": "双唇闭合，然后突然张开，送气爆破音",
            "m": "双唇闭合，鼻腔发出声音",
            "f": "上齿轻触下唇，气流从缝隙中摩擦而出",
            "d": "舌尖抵住上齿龈，然后突然离开，发出爆破音",
            "t": "舌尖抵住上齿龈，然后突然离开，送气爆破音",
            "n": "舌尖抵住上齿龈，鼻腔发出声音",
            "l": "舌尖抵住上齿龈，气流从舌两侧流出",
            "g": "舌根抵住软腭，然后突然离开，发出爆破音",
            "k": "舌根抵住软腭，然后突然离开，送气爆破音",
            "h": "舌根接近软腭，气流摩擦而出",
            "j": "舌面前部抵住硬腭前部，发出塞擦音",
            "q": "舌面前部抵住硬腭前部，送气塞擦音",
            "x": "舌面前部接近硬腭前部，气流摩擦而出",
            "zh": "舌尖后翘抵住硬腭前部，发出塞擦音",
            "ch": "舌尖后翘抵住硬腭前部，送气塞擦音",
            "sh": "舌尖后翘接近硬腭前部，气流摩擦而出",
            "r": "舌尖后翘接近硬腭前部，声带振动",
            "z": "舌尖前伸抵住上齿龈，发出塞擦音",
            "c": "舌尖前伸抵住上齿龈，送气塞擦音",
            "s": "舌尖前伸接近上齿龈，气流摩擦而出",
            "y": "发音类似i，但更短促",
            "w": "发音类似u，但更短促",
            "a": "口腔大开，舌位最低，自然发音",
            "o": "口腔半开，舌位半高，嘴唇圆拢",
            "e": "口腔半开，舌位半高，嘴唇不圆",
            "i": "口腔微开，舌位最高，嘴唇扁平",
            "u": "口腔微开，舌位最高，嘴唇圆拢",
            "ü": "口腔微开，舌位最高，嘴唇撮圆",
            "ai": "从a滑向i",
            "ei": "从e滑向i",
            "ui": "从u滑向i",
            "ao": "从a滑向o",
            "ou": "从o滑向u",
            "iu": "从i滑向u",
            "ie": "从i滑向e",
            "üe": "从ü滑向e",
            "er": "舌尖卷起，发音时舌位变化",
            "an": "从a滑向n",
            "en": "从e滑向n",
            "in": "从i滑向n",
            "un": "从u滑向n",
            "ün": "从ü滑向n",
            "ang": "从a滑向后鼻音ng",
            "eng": "从e滑向后鼻音ng",
            "ing": "从i滑向后鼻音ng",
            "ong": "从o滑向后鼻音ng",
        }
        
        # 整体认读音节使用基本发音方法
        if letter in methods:
            return methods[letter]
        else:
            return "整体认读，不需要拼读"

# 主函数
def main():
    tool = PinyinLearningTool()
    running = True
    
    while running:
        mouse_pos = pygame.mouse.get_pos()
        
        # 更新按钮悬停状态
        for button in tool.buttons:
            button.update_hover(mouse_pos)
        
        # 更新卡片悬停状态
        for card in tool.cards:
            card.highlight = card.rect.collidepoint(mouse_pos)
        
        for event in pygame.event.get():
            if event.type == QUIT:
                running = False
            elif event.type == MOUSEBUTTONDOWN:
                if event.button == 1:  # 左键点击
                    # 检查按钮点击
                    for button in tool.buttons:
                        if button.is_clicked(event.pos):
                            tool.set_category(button.text)
                    
                    # 检查卡片点击
                    for card in tool.cards:
                        if card.rect.collidepoint(event.pos):
                            tool.selected_card = card
                    
                    # 检查详情卡片关闭按钮
                    if tool.selected_card:
                        close_button = tool.draw_detail(screen)
                        if close_button and close_button.is_clicked(event.pos):
                            tool.selected_card = None
        
        # 绘制
        screen.fill(BACKGROUND)
        tool.draw(screen)
        
        # 更新显示
        pygame.display.flip()
        
        # 控制帧率
        clock.tick(FPS)
    
    pygame.quit()
    sys.exit()

if __name__ == "__main__":
    main()